<?php
namespace app\system\model;

use think\Model;

class ManageModel extends Model
{
    protected $table='__MANAGE__';

    /**
     * @param array $where
     * @param bool $pageSize
     * @param string $order
     * @return false|\PDOStatement|string|\think\Collection|\think\Paginator
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function selectManage(array $where=[], $pageSize=false, $order='id desc')
    {
        $fields = 'm.*,g.title groupname';
        $this->table('__MANAGE__')->alias('m')
            ->join('__GROUP__ g', 'g.id=m.group_id')
            ->where($where)
            ->order($order)
            ->field($fields);
        if ($pageSize) {
            $result = $this->paginate($pageSize);
        } else {
            $result = $this->select();
        }
        return $result;
    }

    public function addManage(array $data)
    {
        $data['time'] = time();
        return $this->insert($data);
    }

    public function updateManage(array $data, array $where)
    {
        return $this->where($where)->update($data);
    }

    /**
     * @param array $where
     * @param bool $fields
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function findManage(array $where, $fields=false)
    {
        return $this->field($fields)->where($where)->find();
    }

    public function delManage($id) {
        return $this->where(['id'=>$id])->delete();
    }
}